package cn.new7as.cas5s.ui.home;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.List;

import cn.new7as.cas5s.ui.repay.RepaysActivity;
import cn.new7as.cas5s.ui.slideshow.MainActivity;
import cn.new7as.cas5s.R;
import cn.new7as.cas5s.ui.easy.netapi.OrderApi;
import cn.new7as.cas5s.ui.easy.netapi.ProductCateApi;
import cn.new7as.cas5s.ui.repay.OrdersDetailActivity;

public class Gridadapter extends BaseAdapter {

    private List<ProductCateApi.ProductCateData> list;
    private MainActivity context;


    public Gridadapter(List<ProductCateApi.ProductCateData> list, MainActivity context) {
        super();

        this.context = context;
        this.list = list;

    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder viewHolder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.layout_grid_item, parent, false);
            viewHolder = new ViewHolder();
            viewHolder.itembut = (TextView) convertView.findViewById(R.id.getnow);
            viewHolder.amot = (TextView) convertView.findViewById(R.id.amout);
            viewHolder.proname = (TextView) convertView.findViewById(R.id.proname);

            convertView.setTag(viewHolder);

        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        viewHolder.amot.setText(list.get(position).amountRange.replace("₹", ""));
        viewHolder.proname.setText(list.get(position).productCategorySubName);
        switch (list.get(position).loanStatus) {
            case 0:

                break;
            case 1:
                viewHolder.itembut.setText("Pending");
                break;
            case 2:
                viewHolder.itembut.setText("Repay");

                break;
            case 3:
                viewHolder.itembut.setText("Overdue");
                break;
            case 4:
                viewHolder.itembut.setText("Payment Failed");
                break;   case 5:
                viewHolder.itembut.setText("Payment Failed");
                break;
        }
        ViewHolder finalViewHolder = viewHolder;
        viewHolder.itembut.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                context.show();
                switch (list.get(position).loanStatus) {
                    case 0:
                        context.status(list.get(position).productCategorySubCode);
                        break;
                    case 1:
                        godeta(list.get(position).productCategorySubCode, false);
                        break;
                    case 2:
                        godeta(list.get(position).productCategorySubCode, true);
                        break;
                    case 3:
                        godeta(list.get(position).productCategorySubCode, true);
                        break;
                    case 4:
                        godeta(list.get(position).productCategorySubCode, false);
                        break;
                }
            }
        });
        return convertView;
    }

    void godeta(String pcode, boolean aa) {
        OrderApi.Data data = new OrderApi().new Data();
        Intent intent;

        if (aa) {
            intent = new Intent(context, RepaysActivity.class);

        } else
            intent = new Intent(context, OrdersDetailActivity.class);
        Bundle bundle = new Bundle();
        data.fo = 0;
        data.productCategorySubCode = pcode;

        bundle.putSerializable("ddba", data);
        intent.putExtras(bundle);
        context.startActivity(intent);
    }


    final class ViewHolder {
        TextView itembut;
        TextView proname;
        TextView amot;
    }
}
